//
//  CDSelectLogoViewController.m
//  CollectToDesktop
//
//  Created by 王启颖 on 2020/10/17.
//  Copyright © 2020 godbe. All rights reserved.
//

#import "CDSelectLogoViewController.h"
#import "YNPageViewController.h"
#import "CDSelectLogoDetialViewController.h"
@interface CDSelectLogoViewController ()<YNPageViewControllerDataSource, YNPageViewControllerDelegate>
@property (strong, nonatomic) NSMutableArray *dataArr;

@property (strong, nonatomic) NSMutableArray *classificationNameArr;
@end

@implementation CDSelectLogoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"选择图标";
    
    NSString *file = [[NSBundle mainBundle] pathForResource:@"Slide" ofType:@"plist"];
    NSMutableDictionary*dataDic = [[NSMutableDictionary alloc]initWithContentsOfFile:file];
    NSLog(@"%@",dataDic);
    self.dataArr = [NSMutableArray arrayWithArray:dataDic[@"slide"]];
    
    
    [self setupPageVC];

    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setImage:[UIImage imageNamed:@"返 回(2)"] forState:UIControlStateNormal];
    [button setFrame:CGRectMake(0, 0, 60, 40)];
    button.contentEdgeInsets = UIEdgeInsetsMake(0, -20,0, 0);
    [button addTarget:self action:@selector(navigationItemBack) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithCustomView:button];
    self.navigationItem.leftBarButtonItem =  item;
    
    [self request];
}

-(void)navigationItemBack{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)setupPageVC {
    
    YNPageConfigration *configration = [YNPageConfigration defaultConfig];
    configration.pageStyle = YNPageStyleSuspensionTop;
    configration.headerViewCouldScale = YES;
    /// 控制tabbar 和 nav
    configration.showTabbar = NO;
    configration.showNavigation = YES;
    configration.scrollMenu = YES;
    configration.aligmentModeCenter = NO;
    configration.lineWidthEqualFontWidth = YES;
    configration.showBottomLine = YES;
    configration.lineColor = [UIColor clearColor];
    configration.selectedItemColor = [UIColor whiteColor];
    /// 设置悬浮停顿偏移量
    configration.bottomLineBgColor = [UIColor clearColor];
    configration.itemMargin = 15;
    configration.scrollViewBackgroundColor = [UIColor whiteColor];
    configration.converColor = [UIColor jg_colorHex:0x00C35E];
    configration.showConver = YES;
    
    YNPageViewController *vc = [YNPageViewController pageViewControllerWithControllers:self.getArrayVCs
                                                                                titles:[self getArrayTitles]
                                                                                config:configration];
    vc.dataSource = self;
    vc.delegate = self;
  
    
    UIView *topView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kDeviceWidth, 10)];
    topView.backgroundColor = [UIColor jg_colorHex:0xF0F0F0];
    vc.headerView = topView;

    /// 作为自控制器加入到当前控制器
    [vc addSelfToParentViewController:self];
    
    /// 如果隐藏了导航条可以 适当改y值
    //    pageVC.view.yn_y = kYNPAGE_NAVHEIGHT;
    
//    [self.view addSubview:self.navView];
}

- (NSArray *)getArrayVCs {
    NSMutableArray *arr = [[NSMutableArray alloc]init];


    
    if (self.classificationNameArr.count) {
        for (int i = 0; i < self.classificationNameArr.count; i++) {
            NSDictionary *dic = self.classificationNameArr[i];
            CDSelectLogoDetialViewController *vc = [[CDSelectLogoDetialViewController alloc] init];
            vc.logoClickBlock = ^(UIImage * _Nonnull img) {
                !self.logoClickBlock?:self.logoClickBlock(img);
            };
            vc.nameID = dic[@"id"];
            [arr addObject:vc];
        }
    } else {
        for (int i = 0; i < self.dataArr.count; i++) {
            NSDictionary *dic = self.dataArr[i];
            CDSelectLogoDetialViewController *vc = [[CDSelectLogoDetialViewController alloc] init];
            vc.logoClickBlock = ^(UIImage * _Nonnull img) {
                !self.logoClickBlock?:self.logoClickBlock(img);
            };
            vc.dataArr = dic[@"slideIcon"];
            [arr addObject:vc];
        }
    }
    
    return arr;
}

- (NSArray *)getArrayTitles {
    NSMutableArray *arr = [[NSMutableArray alloc]init];

    
    
    if (self.classificationNameArr.count) {
        for (int i = 0; i < self.classificationNameArr.count; i++) {
            NSDictionary *dic = self.classificationNameArr[i];
            [arr addObject:dic[@"classificationName"]];
        }
    } else {
        for (int i = 0; i < self.dataArr.count; i++) {
            NSDictionary *dic = self.dataArr[i];
            [arr addObject:dic[@"slideTile"]];
        }
    }
    
    
    
    return arr;
}



#pragma mark - YNPageViewControllerDataSource
- (UIScrollView *)pageViewController:(YNPageViewController *)pageViewController pageForIndex:(NSInteger)index {
    CDSelectLogoDetialViewController *vc = pageViewController.controllersM[index];

    return [(CDSelectLogoDetialViewController *)vc collectionView];

}

#pragma mark - YNPageViewControllerDelegate
- (void)pageViewController:(YNPageViewController *)pageViewController
            contentOffsetY:(CGFloat)contentOffset
                  progress:(CGFloat)progress {
//    NSLog(@"--- contentOffset = %f, progress = %f", contentOffset, progress);
//    self.navView.backgroundColor = UIColor.blackColor;
}



-(NSMutableArray *)dataArr{
    if (!_dataArr) {
        _dataArr = [[NSMutableArray alloc]init];
    }
    return _dataArr;
}
-(NSMutableArray *)classificationNameArr{
    if (!_classificationNameArr) {
        _classificationNameArr = [[NSMutableArray alloc]init];
    }
    return _classificationNameArr;
}


#pragma mark - 网络请求
-(void)request{

    NSString *urlstr=[NSString stringWithFormat:@"https://gapi.szlzyd.com/0010/na/icon/iconClassification"];
    [[CDNetworkRequset HttpRequstManager] PostURL:urlstr params:[[NSMutableDictionary alloc]init] success:^(NSInteger code, id  _Nonnull result) {

        self.classificationNameArr = result[@"data"];
        [self setupPageVC];
    } fail:^(NSError * _Nonnull error) {
        
    }];
}


@end
